package com.qqtech.qquan.mall.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallCategoryStatusDictEnum;
import com.qqtech.qquan.mall.model.MallCategory;
import com.qqtech.qquan.mall.model.MallCategoryQuery;
import com.qqtech.qquan.mall.model.MallCategoryVo;
import com.qqtech.qquan.mall.service.MallCategoryService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/category")
public class MallCategoryController extends BaseWebController<MallCategory, MallCategoryQuery> {
	@Resource
	private MallCategoryService mallCategoryService;
    
	@Override
	protected BaseService<MallCategory> getBaseService() {
		return mallCategoryService;
	}
	
	@RequestMapping(value = "/firstLevel")
	@ResponseBody
	public Result firstLevel(@RequestBody MallCategoryQuery mallCategoryQuery) {
		Result result = new Result();
	    List<MallCategoryVo> categoryList = mallCategoryService.queryFirstLevelList(MallCategoryStatusDictEnum.VALID.getCode());	
	    result.put("categoryList", categoryList);
		if(categoryList != null && categoryList.size() > 0){
			MallCategoryVo defaultSelect = categoryList.get(0);
			List<MallCategoryVo> defaultChildrenList = mallCategoryService
					.queryListByParentId(defaultSelect.getCategoryId(),
							MallCategoryStatusDictEnum.VALID.getCode());
			result.put("defaultChildrenList", defaultChildrenList);
		}
		return result;
	}
	
	@RequestMapping(value = "/childrenList")
	@ResponseBody
	public Result childrenList(@RequestBody MallCategoryQuery mallCategoryQuery) {
		Result result = new Result();
		if(mallCategoryQuery == null || mallCategoryQuery.getParentId() == null){
			result.addErr("参数错误");
			return result;
		}
		List<MallCategoryVo> childrenList = mallCategoryService
				.queryListByParentId(mallCategoryQuery.getParentId(),
						MallCategoryStatusDictEnum.VALID.getCode());
		result.put("childrenList", childrenList);
		return result;
	}
}
